﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Reflection;
using System.Web.UI;
using System.Text;

using FRL.DebtChased.Extensions;

namespace FRL.DebtChased.Website
{

	/// <summary>
	/// Declaration of ExtensionsForWebsites class
	/// </summary>
	public static class ExtensionsForWebsites
	{

		/////////////////////////////////////////////////////////////
		// Extensions for building an errors from validator messages

		public static string ToErrorMessage(this ValidatorCollection valCollection)
		{

			// Build a string
			StringBuilder sb = new StringBuilder();
			foreach (IValidator val in valCollection)
			{
				if (!val.IsValid)
				{
					if (sb.Length > 0)
						sb.Append("  ");
					sb.Append(val.ErrorMessage);
				}
			}

			// Return string
			return sb.ToString();

		}

		/////////////////////////////////////////////////////////////
		// Extensions for binding dictionaries to dropdown lists

		public static void BindToDictionary(this DropDownList cbo, IDictionary<string, string> dict)
		{

			// Setup data source
			cbo.DataSource = dict;
			cbo.DataTextField = "Value";
			cbo.DataValueField = "Key";

			// Bind to data source
			cbo.DataBind();

		}

		public static void BindToDictionary(this DropDownList cbo, IDictionary<int, string> dict, int iSelectedKey = -1)
		{

			// Setup data source
			cbo.DataSource = dict;
			cbo.DataTextField = "Value";
			cbo.DataValueField = "Key";

			// Bind to data source
			cbo.DataBind();

			// If there is a key to selected...
			if (iSelectedKey != -1)
			{

				// get the index of the selected key
				string strSelectedKey = iSelectedKey.ToString();
				for (int i1 = 0; i1 < cbo.Items.Count; i1++)
				{
					if (cbo.Items[i1].Value == strSelectedKey)
					{
						cbo.SelectedIndex = i1;
						break;
					}
				}
				
			}

		}

		public static void BindToDictionary(this CheckBoxList chkList, IDictionary<int, string> dict)
		{

			// Setup data source
			chkList.DataSource = dict;
			chkList.DataTextField = "Value";
			chkList.DataValueField = "Key";

			// Bind to data source
			chkList.DataBind();

		}

		public static void BindAndSelected(this DropDownList cboCtrl, Type enumeration, int iSelectedValue, int iIgnore = -1)
		{

			// Get names and values
			string[] names = Enum.GetNames(enumeration);
			Array values = Enum.GetValues(enumeration);

			// Build dictionary
			for (int i1 = 0; i1 < names.Length; i1++)
			{

				// Get the value
				int iValue = (int)values.GetValue(i1);

				// Some values are to be ignored...
				if (iValue == iIgnore)
					continue;

				// Construct a new item
				ListItem li = new ListItem(names[i1], iValue.ToString());
				li.Selected = (iValue == iSelectedValue);

				// Add to list
				cboCtrl.Items.Add(li);

			}

		}

		/////////////////////////////////////////////////////////////
		// Extensions for loading times

		public static Dictionary<int, string> LoadTimeMinutes()
		{

			// Declare return variable
			Dictionary<int, string> Ftmp = new Dictionary<int, string>();

			// Load the dictionary
			for (int iHour = 0; iHour < 24; iHour++)
			{

				// Add the hour
				Ftmp.Add(iHour * 60, (iHour * 60).ToTimeMinutesString());

				// Add the half hour
				Ftmp.Add( (iHour * 60)+30, ((iHour * 60)+30).ToTimeMinutesString());

			}

			// Return variable
			return Ftmp;

		}

		/////////////////////////////////////////////////////////////
		// Extensions for reloading a page

		public static void Reload(this System.Web.UI.Page page)
		{

			// Reload the page
			page.Response.Redirect(page.AppRelativeVirtualPath);

		}

		public static Page GetParentPage(this Control ctrl)
		{

			// Loop up the parents
			Control parentControl = ctrl.Parent;
			while (
				(parentControl!=null)
				&&
				(!(parentControl is Page))
			)
				parentControl = parentControl.Parent;

			// Return parent as a Page object
			return parentControl as Page;

		}

		public static Control GetParentObject(this Control ctrl, Type type)
		{

			// Loop up the parents
			Control parentControl = ctrl.Parent;
			while (
				(parentControl != null)
				&&
				(parentControl.GetType() != type)
			)
				parentControl = parentControl.Parent;

			// Return parent as a Page object
			return parentControl;

		}

		public static void SetReadOnlyAndStyle(this TextBox txtBox, bool bReadOnly)
		{

			// if setting readonly...
			if (bReadOnly)
			{

				// Update textbox
				txtBox.ReadOnly = true;
				txtBox.BorderStyle = BorderStyle.None;
				txtBox.Font.Bold = true;

			}
			else
			{

				// Update textbox
				txtBox.ReadOnly = false;
				txtBox.BorderStyle = BorderStyle.NotSet;

			}

		}

		/////////////////////////////////////////////////////////////
		// Format a plural integer value

		public static string PluralFormat( this int iValue, string strSingle, string strPlural )
		{

			// Format with plural word
			if (iValue == 1)
				return string.Format("1 {0}", strSingle);
			else
				return string.Format("{0} {1}", iValue, strPlural);

		}

		/////////////////////////////////////////////////////////////
		// Get string description of an enumeration value

		public static string GetDescription(this Enum currentEnum)
		{

			// Declare return variable
			string strFtmp = String.Empty;

			// Get the attribute
			FieldInfo fi = currentEnum.GetType().GetField(currentEnum.ToString());
			DescriptionAttribute da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi,typeof(DescriptionAttribute));
			if (da != null)
				strFtmp = da.Description;
			else
				strFtmp = currentEnum.ToString();

			// Return variable
			return strFtmp;

		} 

	}

}